
package com.itheima.controller.backend.security;

import com.itheima.mapper.UserMapper;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
public class SpringSecurityUserService implements UserDetailsService {
    //查找服务，实现查询数据库
    @Autowired
    private UserService userService;
    @Autowired
    private UserMapper userMapper;

    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //调用用户服务获取用户信息

            List<User> list = userService.getUser(username);
            User user = list.get(0);
            List<Role> roles = userService.getRoleByUserId(user.getId());
            HashSet<GrantedAuthority> set = new HashSet<>();
            for (Role role : roles) {
                set.add(()->role.getKeyword());
            }
            return new org.springframework.security.core.userdetails.User(username,user.getPassword(),set);

    }
}

